package main.java.反射4;

import java.lang.reflect.Method;

/**
 * @Auther:Liu
 * @Testname:Test
 * @Date:2025/3/13 14:31
 */
public class Test {
    public static void main(String[] args)throws Exception {
        //一般
        Teacher teacher = new Teacher();
        String s = use1(teacher);
        System.out.println(s);
        System.out.println("_______________________");
        //反射
        Student student = new Student();
        String use = use(student);
        System.out.println(use);
    }

    //
    public static String use1(Object obj){
        if (obj instanceof Student){
            Student student=(Student)obj;
            return student.getuse("张三");
        }
        if (obj instanceof Teacher){
            Teacher teacher=(Teacher)obj;
            return teacher.getuse("张三");
        }
        return null;
    }


    //反射
    public static String use(Object obj)throws Exception{
        Class<?> aClass = obj.getClass();
        Object o = aClass.newInstance();
        Method getuse = aClass.getDeclaredMethod("getuse", String.class);
        return  (String)getuse.invoke(o, "李四");
    }
}
